﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.LIB;
using YArchitech.Revit;

namespace HYRElectric.PowerSystem
{
	public partial class EquipExchangeRefreshListForm : System.Windows.Forms.Form
	{
		public EquipExchangeRefreshListForm(ExternalCommandData cmdData)
		{
			this.InitializeComponent();
			this.m_uiDoc = cmdData.Application.ActiveUIDocument;
			this.m_doc = this.m_uiDoc.Document;
			this.m_lstLinkDocs = new List<Document>();
			this.m_lstElecEquipsInValidInLinkDoc = new List<Element>();
		}

		private void EquipExchangeRefreshListForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.GetTolNoDevice();
			this.InitData();
		}

		private EquipExchangeRefreshListForm.EquipCheckType GetCheckType(FamilyInstance fiInstance, ref EquipExchangeXMLSetting xmlSetting, Document curDoc)
		{
			double num = 0.0;
			XYZ xyz = null;
			EquipExchangeExternalHandler.GetLinkEleCurDocLocation(ref xyz, ref num, curDoc, fiInstance);
			EquipExchangeRefreshListForm.EquipCheckType result = EquipExchangeRefreshListForm.EquipCheckType.Added;
			foreach (LinkEleItem linkEleItem in xmlSetting.m_lstLinkEleSet)
			{
				if (fiInstance.Id.IntegerValue == linkEleItem.m_lID)
				{
					ElementId elementId = new ElementId(linkEleItem.m_lIDCurExchange);
					Element element = this.m_doc.GetElement(elementId);
					if (DlgPanelSystemDetect.GetElecEquipPositionInfo(ref num, element).IsAlmostEqualTo(xyz))
					{
						return EquipExchangeRefreshListForm.EquipCheckType.None;
					}
					return EquipExchangeRefreshListForm.EquipCheckType.Moved;
				}
			}
			return result;
		}

		private void DeleteNoLinkEquip(EquipExchangeXMLSetting xmlSetting)
		{
			List<Element> list = new List<Element>();
			foreach (LinkEleItem linkEleItem in xmlSetting.m_lstLinkEleSet)
			{
				bool flag = false;
				using (List<Element>.Enumerator enumerator2 = this.m_lstElecEquipsInValidInLinkDoc.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						if (enumerator2.Current.Id.IntegerValue == linkEleItem.m_lID)
						{
							flag = true;
							break;
						}
					}
				}
				if (!flag)
				{
					ElementId elementId = new ElementId(linkEleItem.m_lIDCurExchange);
					if (!(elementId == null))
					{
						Element element = this.m_doc.GetElement(elementId);
						if (element != null)
						{
							list.Add(element);
						}
					}
				}
			}
			if (list.Count == 0)
			{
				return;
			}
			string text = list.Count.ToString() + "个替换图例原链接文件设备已经删除，是否删除当前设备图例？";
			if (DialogResult.Yes != MessageBox.Show(text, "提示", MessageBoxButtons.YesNo))
			{
				return;
			}
			Transaction transaction = new Transaction(this.m_doc);
			transaction.Start("delNoLinkEquip");
			foreach (Element elem in list)
			{
				this.m_doc.DeleteElement(elem);
			}
			transaction.Commit();
		}

		private void InitData()
		{
			TreeNode treeNode = this.tvwCheckList.Nodes.Add("提资刷新");
			TreeNode treeNode2 = treeNode.Nodes.Add("增加");
			TreeNode treeNode3 = treeNode.Nodes.Add("移动");
			EquipExchangeXMLSetting xmlSetting = null;
			DlgPanelSystemDetect.InitEquipExchangeXML(ref xmlSetting);
			foreach (Element element in this.m_lstElecEquipsInValidInLinkDoc)
			{
				FamilyInstance familyInstance = element as FamilyInstance;
				if (familyInstance != null)
				{
					EquipExchangeRefreshListForm.EquipCheckType checkType = this.GetCheckType(familyInstance, ref xmlSetting, this.m_doc);
					string familyInstanceName = familyInstance.GetFamilyInstanceName();
					TreeNode treeNode4;
					if (checkType == EquipExchangeRefreshListForm.EquipCheckType.Added)
					{
						treeNode4 = treeNode2.Nodes.Add(familyInstanceName);
					}
					else
					{
						if (checkType != EquipExchangeRefreshListForm.EquipCheckType.Moved)
						{
							continue;
						}
						treeNode4 = treeNode3.Nodes.Add(familyInstanceName);
					}
					treeNode4.Tag = familyInstance;
					treeNode4.ImageIndex = 1;
					treeNode4.SelectedImageIndex = 1;
				}
			}
			this.tvwCheckList.ExpandAll();
			this.DeleteNoLinkEquip(xmlSetting);
		}

		private void GetTolNoDevice()
		{
			List<Element> list = new List<Element>();
			IEnumerable<Document> linkDocuments = this.m_doc.GetLinkDocuments();
			this.m_lstLinkDocs.Clear();
			if (linkDocuments != null)
			{
				foreach (Document document in linkDocuments)
				{
					if (document != null)
					{
						this.m_lstLinkDocs.Add(document);
						List<FamilyInstance> linkDocElemsByViewOutline = DlgPanelSystemDetect.GetLinkDocElemsByViewOutline(document, this.m_doc.ActiveView);
						list.AddRange(linkDocElemsByViewOutline);
					}
				}
			}
			List<Element> list2 = new List<Element>();
			DlgPanelSystemDetect.InitElecEquipData(list, ref list2, ref this.m_lstElecEquipsInValidInLinkDoc);
		}

		private void tvwCheckList_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			if (e.Node.Nodes.Count > 0)
			{
				return;
			}
			FamilyInstance familyInstance = e.Node.Tag as FamilyInstance;
			if (familyInstance == null)
			{
				return;
			}
			if (familyInstance.Document == this.m_doc)
			{
				Selection selection = this.m_uiDoc.Selection;
				Common.SetElementVisibility(familyInstance, this.m_doc);
				RevitVersionFuncs.ClearSelection(selection);
				RevitVersionFuncs.AddToSelection(selection, familyInstance);
				this.m_uiDoc.ShowElements(familyInstance);
				return;
			}
			DlgPanelSystemDetect.ZoomCenter(familyInstance, this.m_uiDoc);
		}

		private Document m_doc;

		private UIDocument m_uiDoc;

		private List<Document> m_lstLinkDocs;

		private List<Element> m_lstElecEquipsInValidInLinkDoc;

		private enum EquipCheckType
		{
			None,
			Added,
			Moved,
			Deleted
		}
	}
}
